//
//  FlyScrollView.m
//  JiongTu
//
//  Created by Dupengfei on 16/3/11.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "FlyScrollView.h"
#import "ControllerManger.h"
#import "ADViewController.h"
static int const ImageViewCount = 3;

@interface FlyScrollView ()<UIScrollViewDelegate>
@property (weak, nonatomic) UIScrollView *scrollView;
/* 定时器 */
@property (weak, nonatomic) NSTimer *timer;

@property (weak, nonatomic, readonly) UIPageControl *pageControl;

@end


@implementation FlyScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        //图片默认切换时间
        self.time = 2;
        
        // 滚动视图
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.pagingEnabled = YES;
        scrollView.bounces = NO;
        scrollView.delegate = self;
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        // 图片控件
        for (int i = 0; i<ImageViewCount; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            [scrollView addSubview:imageView];
             imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnMask:)];
           
            [imageView addGestureRecognizer:tap];
           
        }
        
        // 页码视图
        UIPageControl *pageControl = [[UIPageControl alloc] init];

        pageControl.currentPageIndicatorTintColor=[UIColor greenColor];
        [self addSubview:pageControl];
        _pageControl = pageControl;
        
    }
    return self;
    
    
    
}

#pragma mark - 布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 设置frame
    self.scrollView.frame = self.bounds;
    
    // 设置contentSize
    if (self.isInfiniteScrollVertical) {
        self.scrollView.contentSize = CGSizeMake(0, ImageViewCount * self.bounds.size.height);
    } else {
        self.scrollView.contentSize = CGSizeMake(ImageViewCount * self.bounds.size.width, 0);
    }
    
    // 摆放imageView
    for (int i = 0; i<ImageViewCount; i++) {
        UIImageView *imageView = self.scrollView.subviews[i];
        
        if (self.isInfiniteScrollVertical) {
            imageView.frame = CGRectMake(0, i * self.scrollView.frame.size.height, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
            
            
            
        } else {
            imageView.frame = CGRectMake(i * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
            
//            NSLog(@"++++++%f",self.scrollView.frame.size.width);
        }
    }
    
    // 设置pageControl
    CGFloat pageW = 80;
    CGFloat pageH = 20;
    CGFloat pageX = self.scrollView.frame.size.width - pageW;
    CGFloat pageY = self.scrollView.frame.size.height - pageH;
    self.pageControl.frame = CGRectMake(pageX, pageY, pageW, pageH);
    
    [self updateContent];
}

#pragma mark - setter
- (void)setImages:(NSArray *)images
{
    _images = images;
    
    // 设置页码
    self.pageControl.numberOfPages = images.count;
    
    self.pageControl.currentPage = 0;
    
    // 设置内容
    [self updateContent];
    
    // 开始定时器
    [self startTimer];
}

- (void)setTime:(CGFloat)time{
    
    _time = time;
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 计算当前页码（找出当前显示在最中间的imageView）
    NSInteger page = 0;
    CGFloat minDistance = MAXFLOAT;
    for (int i = 0; i<ImageViewCount; i++) {
        UIImageView *imageView = self.scrollView.subviews[i];
        CGFloat distance = 0;
        if (self.isInfiniteScrollVertical) {
            distance = ABS(imageView.frame.origin.y - scrollView.contentOffset.y);
        } else {
            distance = ABS(imageView.frame.origin.x - scrollView.contentOffset.x);
        }
        if (distance < minDistance) {
            minDistance = distance;
            page = imageView.tag;
        }
    }
    self.pageControl.currentPage = page;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self startTimer];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self updateContent];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self updateContent];
}

#pragma mark - 内容更新
- (void)updateContent
{
    // 设置图片
    for (int i = 0; i<ImageViewCount; i++) {
        UIImageView *imageView = self.scrollView.subviews[i];
        // 当前页码
        NSInteger index = self.pageControl.currentPage;
        if (i == 0) { // 左边
            index--;
        } else if (i == 2) { // 右边
            index++;
        }
        // 越界处理
        if (index < 0) {
            index = self.pageControl.numberOfPages - 1;
        } else if (index >= self.pageControl.numberOfPages) {
            index = 0;
        }
        imageView.tag = index;
        imageView.image = self.images[index];
        
        
    }
    
    // 设置偏移量在中间
    if (self.isInfiniteScrollVertical) {
        self.scrollView.contentOffset = CGPointMake(0, self.scrollView.frame.size.height);
    } else {
        self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
    }
}

#pragma mark - 定时器处理
- (void)startTimer
{
    NSTimer *timer = [NSTimer timerWithTimeInterval:self.time target:self selector:@selector(next) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
}

- (void)stopTimer
{
    [self.timer invalidate];
    self.timer = nil;
}
//下一张图片
- (void)next
{
    if (self.isInfiniteScrollVertical) {
        [self.scrollView setContentOffset:CGPointMake(0, 2 * self.scrollView.frame.size.height) animated:YES];
    } else {
        [self.scrollView setContentOffset:CGPointMake(2 * self.scrollView.frame.size.width, 0) animated:YES];
    }
}

//广告
- (void)tapOnMask:(UITapGestureRecognizer *)sender
{
    ADViewController * view =[[ADViewController alloc]init];
    switch (self.pageControl.currentPage) {
        case 0:
        {
            [[ControllerManger sharedManager].homeVC.navigationController pushViewController:view animated:YES];
            view.url =[NSURL URLWithString:@"http://ac-l8ufg3xl.clouddn.com/c814af5e4250e3b7.jpg"];
        }
            break;
        case 1:
        {
            [[ControllerManger sharedManager].homeVC.navigationController pushViewController:view animated:YES];
            view.url =[NSURL URLWithString:@"http://ac-l8ufg3xl.clouddn.com/77043779ce8369fd.jpg"];
        }
            break;
        case 2:
        {
            [[ControllerManger sharedManager].homeVC.navigationController pushViewController:view animated:YES];
            view.url =[NSURL URLWithString:@"http://ac-l8ufg3xl.clouddn.com/a37c565059f9a0cb.jpg"];
        }
        break;
            
        default:
            break;
    }
   
    
}


@end
